﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace 代码生成器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public DataTable ExecuteDataTable(string commandText,
            params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(txtConnection.Text))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = commandText;
                    cmd.Parameters.AddRange(parameters);
                    DataTable dt = new DataTable();
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(dt);
                    return dt;
                }
            }
        }

        private void btnConnection_Click(object sender, EventArgs e)
        {
            DataTable dt = ExecuteDataTable("Select * from INFORMATION_SCHEMA.TABLES");
            foreach (DataRow row in dt.Rows)
            {
                clbTable.Items.Add((string)row["TABLE_NAME"]);
            }
        }

        public static string ConvertToNetType(string dataType)
        {
            switch (dataType)
            {
                case "int":
                    return "int";
                case "varchar":
                case "nvarchar":
                case "char":
                case "nchar":
                    return "string";
                case "bit":
                    return "bool";
                default:
                    return "object";
            }
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            foreach (string tableName in clbTable.CheckedItems)
            {
                //CreateModle(tableName);

                CreateDAL(tableName);
            }
        }

        private void CreateModle(string tableName)
        {
            //string commandText = "Select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = '";
                //commandText += tableName;
                //commandText += "'";
                //DataTable dt = ExecuteDataTable(commandText);  

            DataTable dt = ExecuteDataTable("select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @tableName", new SqlParameter("tableName", tableName));

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("class " + tableName);
            sb.AppendLine("{");

            foreach (DataRow row in dt.Rows)
            {
                string colName = (string)row["column_name"];
                string type = ConvertToNetType((string)row["data_type"]);
                sb.AppendLine("public " + type + " " + colName + "{ get; set; }");
            }

            sb.AppendLine("}");

            Directory.CreateDirectory(@"c:\自己生成的代码");
            File.WriteAllText(@"c:\自己生成的代码\" + tableName + ".cs", sb.ToString());
        }

        private void CreateDAL(string tableName)
        {
            DataTable dt = ExecuteDataTable("select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @tableName and column_name <> 'id'", new SqlParameter("tableName", tableName));
            string[] cols = new string[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                cols[i] = (string)row["column_name"];
            }

            string[] parameters = new string[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                parameters[i] = "@" + cols[i];
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("class " + tableName + "DAL");
            sb.AppendLine("{");
            sb.AppendLine("   public int addNew(" + tableName + " model)");
            sb.AppendLine("   {");
            sb.AppendLine("      object obj = sqlHelper.ExecuteScalar(");
            sb.AppendLine("         \"insert into " + tableName + "(" + string.Join(",", cols) + ") output inserted.id values(" + string.Join(",",parameters) + "),");
            for(int i=0;i<cols.Length;i++)
            {
                sb.AppendLine("          new SqlParameter(\"" + cols[i] + "\", model." + cols[i] + ")" + (i==(cols.Length-1)?");":","));
            }
            sb.AppendLine("      return Convert.ToInt32(obj);");
            sb.AppendLine("   }");
            sb.AppendLine("}");
            MessageBox.Show(sb.ToString());
        }
    }
}
